//
//  FiltrateAttributeItemCell.m
//  TerritoryShopkeeper
//
//  Created by xiaowu on 2017/12/19.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "FiltrateAttributeItemCell.h"

static NSInteger BaseButtonTag = 13124151;

@interface FiltrateAttributeItemCell ()
@property (nonatomic, strong) NSMutableArray *firstModel;
@end

@implementation FiltrateAttributeItemCell

+ (CGFloat)getHeightWithFirstModel:(FiltrateFirstModel *)firstModel {
    if (!firstModel.isShow) {
        return 0;
    }
    CGFloat height = 0;
    CGFloat itemHeight = 30;
    NSInteger row = (firstModel.secondList.count + 2) / 3;
    height += 10 + (itemHeight + 10) * row;
    return height;
}

- (void)setValueWithFirstModel:(FiltrateFirstModel *)firstModel {
    for (UIButton *button in self.contentView.subviews) {
        [button removeFromSuperview];
    }
    if (!firstModel.isShow) {
        return;
    }
    CGFloat width = (self.contentView.width - 40) / 3.0;
    CGFloat height = 30;
    CGFloat commonOffset = 10;
    
    for (NSInteger i = 0; i < firstModel.secondList.count; i++) {
        FiltrateSecondModel *secondModel = [firstModel.secondList objectAtIndex:i];
        NSInteger index = i % 3;
        NSInteger row = i / 3;
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(commonOffset + (width + commonOffset) * index, commonOffset + (height + commonOffset) * row, width, height);
        
        [button setTitle:secondModel.title forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = TextPingFangFontSize(12);
        
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHex:0xF0F2F5 alpha:1.0]] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor ThemeColor]] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor blackTextColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        button.tag = BaseButtonTag + i;
//        [button setBackgroundColor:[UIColor colorWithHex:0xF0F2F5 alpha:1.0]];
        [self.contentView addSubview:button];
    }
    
}

- (void)buttonClick:(UIButton *)sender {
    if (sender.isSelected) {
        sender.selected = NO;
    } else {
        sender.selected = YES;
    }
    if (self.block) {
        self.block(sender.tag - BaseButtonTag, sender.isSelected);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


@end
